package com.group.service.impl;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.group.dao.TbCustomerMapper;
import com.group.pojo.Common;
import com.group.pojo.TbCustomer;
import com.group.pojo.TbCustomerExample;
import com.group.pojo.TbCustomerExample.Criteria;
import com.group.pojo.TbUser;
import com.group.service.CommonService;
import com.group.service.CustomerService;
import com.group.util.CommonUtil;
import com.group.util.MD5;

@Service
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	private TbCustomerMapper customerMapper;
	@Autowired
	private CommonService commonService;
	// 根据客户id查询客户名称
	@Override
	public String query(String id) {
		String customerName = null;
		List<TbCustomer> list = customerMapper.selectByExample(null);
		for (TbCustomer customer : list) {
			if (customer.getId().equals(id)) {
				return customer.getCustomerName();
			}
		}
		return customerName;
	}

	// 通过id查询客户的详情
	@Override
	public TbCustomer getCustomerInfoById(String id) {
		List<TbCustomer> list = customerMapper.selectByExample(null);
		for (TbCustomer customer : list) {
			if (customer.getId().equals(id)) {
				return customer;
			}
		}
		return null;
	}

	// 查询所有未注销的客户信息
	@Override
	public List<TbCustomer> getAllCustomers() {
		List<TbCustomer> list = customerMapper.selectByExample(null);
		for (int i = 0; i < list.size(); i++) {
			if ("0".equals(list.get(i).getCustomerStatus())) {
				list.remove(i);
				i--;
			}
		}
		return list;
	}

	// 添加新的客户信息
	@Override
	public Map<String, String> saveCustomerInfo(HttpServletRequest request, TbCustomer customer) {
		TbUser loginUser = (TbUser) request.getSession().getAttribute("USER_SESSION");

		Date date = new Date();
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.add(calendar.DATE, 90);
		Date expire = calendar.getTime();

		// 设置创建人、创建时间
		customer.setCreateUserId(loginUser.getId());
		customer.setCreateTime(date);
		customer.setCustomerStatus("1");
		// 密码
		customer.setCustomerPassword(MD5.encrypt("123456"));

		// 唯一编号id
		String nextId = commonService
				.getTbCurrentDateNextId(new Common("tb_customer", CommonUtil.getCurrentDate("yyyyMMdd")));
		customer.setId(nextId);

		// 提交到数据库
		int insert = customerMapper.insertSelective(customer);
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");

		if (insert == 1) {
			returnMap.put("success", "true");
		}
		return returnMap;
	}

	// 更新客户的信息
	@Override
	public Map<String, String> updateCustomerInfo(TbCustomer customer) {
		TbCustomer newCustomer = new TbCustomer();//临时对象
		newCustomer = this.getCustomerInfoById(customer.getId());
		newCustomer.setLoginName(customer.getLoginName());//填充登录名
		newCustomer.setCustomerName(customer.getCustomerName());//填充客户名
		// 提交到数据库
		int insert = customerMapper.updateByPrimaryKeySelective(newCustomer);
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");

		if (insert == 1) {
			returnMap.put("success", "true");
		}
		return returnMap;
	}
	//重置客户登陆密码
	@Override
	public Map<String, String> resetCustomerPassword(String customerId) {
		TbCustomer customer = new TbCustomer();
		customer.setCustomerPassword(MD5.encrypt("123456"));
		Map<String, String> map = CommonUtil.getReturnMap("success", "true");
			customer.setId(customerId);
			int update = customerMapper.updateByPrimaryKeySelective(customer);
			if (update != 1) {
				map.put("success", "false");
			}
		return map;
	}
	

	// 删除客户的信息
	@Override
	public Map<String, String> deleteCustomerInfo(String[] customerIds) {
		TbCustomer customer = new TbCustomer();
		customer.setCustomerStatus("0");
		Map<String, String> map = CommonUtil.getReturnMap("success", "true");
		for (String customerId : customerIds) {
			customer.setId(customerId);
			int update = customerMapper.updateByPrimaryKeySelective(customer);
			if (update != 1) {
				map.put("success", "false");
				return map;
			}
		}
		return map;
	}
    //校验登录名
	@Override
	public String checkLoginName(String loginName) {
		List<TbCustomer> list= customerMapper.selectByExample(null);
		for (TbCustomer customer : list) {
			if(loginName.equals(customer.getLoginName())){
				return "false";
			}
		}
		return "true";
	}
    //通过客户姓名查询客户id
	@Override
	public String getcustomerIdbyName(String customerName) {
		TbCustomerExample example =new  TbCustomerExample();
		example.createCriteria().andCustomerNameEqualTo(customerName).andCustomerStatusEqualTo("1");
		List<TbCustomer> list = customerMapper.selectByExample(example);
		if(list.size()!=0){		
			return list.get(0).getId();
		}else{
			return null;
		}
	}
    //校验登录名是否存在
	@Override
	public String CheckCustomerName(String customerName) {
		List<TbCustomer> list= customerMapper.selectByExample(null);
		for (TbCustomer customer : list) {
			if(customerName.equals(customer.getCustomerName())){
				return "true";
			}
		}
		return "false";
	}

	
	
	
	
	
	
	private void setCondition(Criteria criteria, TbCustomer customer) {
		String id = customer.getId();
		if (id != null && !"".equals(id)) {
			criteria.andIdEqualTo(id);
		}
		
		String customerStatus = customer.getCustomerStatus();
		if (customerStatus!= null && !"".equals(customerStatus)) {
			criteria.andCustomerStatusEqualTo(customerStatus);
		}
		
		String loginName = customer.getLoginName();
		if (loginName != null && !"".equals(loginName)) {
			criteria.andLoginNameEqualTo(loginName);
		}
		
		String customerName = customer.getCustomerName();
		if (customerName!= null && !"".equals(customerName)) {
			criteria.andCustomerNameEqualTo(customerName);
		}
	}
	
	@Override
	public List<TbCustomer> query(TbCustomer customer) {
		TbCustomerExample example = new TbCustomerExample();
		
		setCondition(example.createCriteria(), customer);
		
		List<TbCustomer> list = customerMapper.selectByExample(example);
		return list;
	}


}
